package com.xiaoyu.test;

/**
 * 基于邻接矩阵结构的图
 */
public class MatGraphClass {


    private String head;
    private String[] names;
    private static final int MAX = 100;
    private static final int HAVE = 1;
    //邻阶矩阵数组
    private int[][] edges;
    //用于标记DFS遍历算法已访问的的序号
    private int[] visited = new int[MAX];
    private static final int VISITED = 0;
    private int number, edge;
    //顶点信息
    private String[] vexs;


    public MatGraphClass() {
        edges = new int[MAX][MAX];
        vexs = new String[MAX];
    }

    public void setNames(String[] names) {
        this.names = names;
    }

    public void setHead(String head) {
        this.head = head;
    }

    /**
     * 创建图
     *
     * @param matGraph
     * @param number
     * @param edge
     */
    public void createMatGraph(int matGraph[][], int number, int edge) {
        this.number = number;
        this.edge = edge;
        //遍历传入的矩阵
        for (int i = 0; i < number; i++) {
            //将行传入edges的行
            edges[i] = new int[number];
            for (int j = 0; j < number; j++) {
                //进行赋值
                edges[i][j] = matGraph[i][j];
            }
        }
    }

    /**
     * 展示图信息
     */
    public void displayMatGraph() {
        //外循环是层
        for (int i = 0; i < number; i++) {
            for (int j = 0; j < number; j++) {
                System.out.print(edges[i][j] + " ");
            }
            //换行
            System.out.println();
        }
    }

    /**
     * 基于邻接矩阵的深度优先遍历
     *
     * @param start 选择开始的元素
     */
    public void MatGraphDFS(int start) {

        if (head.equals(names[start])) {
            System.out.println("源节点:" + names[start]);
        } else {
            System.out.println("-接收者:" + names[start]);
        }
        this.visited[start] = 1;
        for (int i = 0; i < number; i++) {
            if (edges[start][i] == HAVE) {
                if (visited[i] == VISITED) {
                    MatGraphDFS(i);
                }
            }
        }
    }

    public void clearVisited() {
        //清空visited
        this.visited = new int[MAX];
    }

}
